<template>
    <div class="chat-container">
        <el-scrollbar class="message-list">
            <div v-for="(message, index) in messages" :key="index" class="message"
                :class="{ 'user-message': message.role === 'user', 'ai-message': message.role === 'ai' }">
                <div class="message-avatar">
                    <el-avatar :src="message.role === 'user' ? userAvatar : aiAvatar" />
                </div>
                <div class="message-content">
                    {{ message.content }}
                </div>
            </div>
        </el-scrollbar>

        <div class="input-area">
            <el-input v-model="inputMessage" type="textarea" :rows="2" placeholder="输入消息..." resize="none"
                @keyup.enter.native="sendMessage" />
            <el-button type="primary" @click="sendMessage" :disabled="!inputMessage.trim()">
                发送
            </el-button>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';

const messages = ref([
    { role: 'ai', content: '你好！我是AI助手，有什么可以帮您的吗？' }
]);
const inputMessage = ref('');
const userAvatar = ref('https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png');
const aiAvatar = ref('https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png');

const sendMessage = () => {
    if (!inputMessage.value.trim()) return;

    messages.value.push({ role: 'user', content: inputMessage.value.trim() });
    inputMessage.value = '';

    // 模拟AI回复
    setTimeout(() => {
        messages.value.push({ role: 'ai', content: '这是AI的回复...' });
    }, 1000);
};
</script>

<style scoped>
.chat-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 16px;
}

.message-list {
    flex: 1;
    margin-bottom: 16px;
    overflow-y: auto;
}

.message {
    margin-bottom: 12px;
    max-width: 80%;
    word-wrap: break-word;
    display: flex;
    gap: 12px;
    align-items: flex-start;
}

.message-content {
    padding: 12px 16px;
    border-radius: 8px;
    line-height: 1.5;
}

.user-message {
    margin-left: auto;
    flex-direction: row-reverse;
}

.user-message .message-content {
    background-color: #409eff;
    color: white;
}

.ai-message {
    margin-right: auto;
}

.ai-message .message-content {
    background-color: #f2f6fc;
    color: #333;
}

.input-area {
    display: flex;
    gap: 12px;
}

.el-textarea {
    flex: 1;
}
</style>